package com.dang.alex.dao.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.dang.alex.dao.daoInterface.OrderDAO;
import com.dang.alex.model.Order;
import com.dang.alex.util.JdbcUtil;

public class OrderDAOImp implements OrderDAO {
	private static final String INSERT= "insert into d_order(user_id,receive_name,full_address,postal_code,mobile,phone,status,order_time,order_desc,total_price) values(?,?,?,?,?,?,?,?,?,?)";

	@Override
	public void save(Order order) {
		PreparedStatement prep = null;
		Connection conn = null;
		
			try {
				conn = JdbcUtil.getConnection();
				prep = conn.prepareStatement(INSERT, Statement.RETURN_GENERATED_KEYS);
				prep.setInt(1, order.getUserId());
				prep.setString(2, order.getReceiceName());
				prep.setString(3, order.getFullAddress());
				prep.setString(4, order.getPostalCode());
				prep.setString(5, order.getMobile());
				prep.setString(6, order.getPhone());
				prep.setInt(7,order.getStatus());
				prep.setLong(8,order.getOrderTime());
				prep.setString(9,order.getOrderDesc());
				prep.setDouble(10, order.getTotalPrice());
				prep.executeUpdate();
				ResultSet rst = prep.getGeneratedKeys();
				rst.next();
				int id = rst.getInt(1);
				order.setId(id);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		finally{
			JdbcUtil.close(prep, conn);
		}
		
	}
	

}
